package top.houry.im.client.utils;

import cn.hutool.core.util.ObjectUtil;
import io.netty.channel.ChannelHandlerContext;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

public class ImClientChannelUtils {

    private static final Set<ChannelHandlerContext> CLIENT_CHANNEL_CACHE_INFO = new CopyOnWriteArraySet<>();

    public static void addChannelHandlerContext(ChannelHandlerContext ctx) {
        CLIENT_CHANNEL_CACHE_INFO.add(ctx);
    }

    public static ChannelHandlerContext getChannelHandlerContext() {
        return CLIENT_CHANNEL_CACHE_INFO.iterator().next();
    }

    public static boolean checkIsAvailable(ChannelHandlerContext ctx) {
        return !ObjectUtil.isNull(ctx) && ctx.channel().isActive();
    }

}
